package com.test18_链表;

import org.junit.Test;

import java.util.Arrays;

class Node<T> {
    private T data;     // 保存所有数据
    private Node next;  // 描述下一个节点

    public Node(T data) {
        this.data = data;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

public class YootkDemo_01 {

    @Test
    public void test1() {
        Node<String> nodeA = new Node<>("edu.yootk.com");
        Node<String> nodeB = new Node<>("www.yootk.com");
        Node<String> nodeC = new Node<>("yootk.ke.qq.com");
        // 设置节点关联
        nodeA.setNext(nodeB);
        nodeB.setNext(nodeC);
        // 节点输出
        print(nodeA);
    }

    /** 获取数据 */
    public static void print(Node node) {
        if (node == null) {     // 结束条件
            return;
        }
        System.out.println(node.getData());
        print(node.getNext());  // 继续输出下一个节点
    }

    @Test
    public void test2() {
        ILink<String> link = new LinkImpl<>();
        System.out.println("当前元素个数：" + link.size() + "、链表状态：" + link.isEmpty());
        link.add("edu.yootk.com");
        link.add("www.yootk.com");
        link.add("yootk.ke.qq.com");
        System.out.println("当前元素个数：" + link.size() + "、链表状态：" + link.isEmpty());
    }

    @Test
    public void test3() {
        ILink<String> link = new LinkImpl<>();
        link.add("edu.yootk.com");
        link.add("www.yootk.com");
        link.add("yootk.ke.qq.com");
        Object[] data = link.toArray();
        for (int x = 0; x < data.length; x++) {
            System.out.println(data[x]);
        }
    }

    @Test
    public void test4() {
        ILink<String> link = new LinkImpl<>();
        link.add("edu.yootk.com");
        link.add("www.yootk.com");
        link.add("yootk.ke.qq.com");
        System.out.println(link.get(1));
        System.out.println(link.get(0));
        System.out.println(link.get(3));
    }

    @Test
    public void test5() {
        ILink<String> link = new LinkImpl<>();
        link.add("edu.yootk.com");
        link.add("www.yootk.com");
        link.add("yootk.ke.qq.com");
        System.out.println("原始内容：" + link.set(1, "沐言优拓：www.yootk.com"));
        Object[] data = link.toArray();
        for (int x = 0; x < data.length; x++) {
            System.out.println(data[x]);
        }
    }

    @Test
    public void test6() {
        ILink<String> link = new LinkImpl<>();
        System.out.println(link.contains("edu.yootk.com"));
        link.add("edu.yootk.com");
        link.add("www.yootk.com");
        link.add("yootk.ke.qq.com");
        System.out.println(link.contains("edu.yootk.com"));
    }

    @Test
    public void test7() {
        ILink<String> link = new LinkImpl<>();
        link.add("edu.yootk.com");
        link.add("www.yootk.com");
        link.add("yootk.ke.qq.com");
        System.out.println(link.remove("www.yootk.com"));
        System.out.println(link.contains("www.yootk.com"));
    }

    @Test
    public void test8() {
        ILink<String> link = new LinkImpl<>();
        link.add("edu.yootk.com");
        link.add("www.yootk.com");
        link.add("yootk.ke.qq.com");
        link.clear();
        Object[] data = link.toArray();
        if (data != null) {
            Arrays.stream(data).forEach(System.out::println);
        }
    }
}
